package com.sora.exception;

import com.sora.common.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Classname DefaultExceptionHandler
 * @Description 默认异常拦截器
 * @Date 2023/4/15 23:00
 * @User by Sora33
 */
@RestControllerAdvice
public class DefaultExceptionHandler {

    private Logger logger = LoggerFactory.getLogger(DefaultExceptionHandler.class);

    @ExceptionHandler(RuntimeException.class)
    public Result RuntimeExceptionHandler(Exception e) {
        logger.error("捕获到运行时异常，异常出现类：【{}】，异常原因：【{}】，异常信息：【{}】", e.getClass(), e.getCause(),e.getMessage());
        return Result.error("服务器内部错误");
    }

    @ExceptionHandler(NumberFormatException.class)
    public Result NumberFormatExceptionHandler(Exception e) {
        logger.error("捕获到数字转换异常，异常出现类：【{}】，异常原因：【{}】，异常信息：【{}】", e.getClass(), e.getCause(),e.getMessage());
        return Result.error("服务器内部错误");
    }

    @ExceptionHandler(NullPointerException.class)
    public Result NullPointerHandler(Exception e) {
        logger.error("捕获到空指针异常，异常出现类：【{}】，异常原因：【{}】，异常信息：【{}】", e.getClass(), e.getCause(),e.getMessage());
        return Result.error("服务器内部错误");
    }

    @ExceptionHandler(IllegalArgumentException .class)
    public Result IllegalArgumentExceptionHandler(Exception e) {
        logger.error("捕获到参数异常异常，异常出现类：【{}】，异常原因：【{}】，异常信息：【{}】", e.getClass(), e.getCause(),e.getMessage());
        return Result.error("服务器内部错误");
    }
}
